package com.jk.lesson5;

import com.jk.bean.Trader;
import com.jk.bean.Transaction;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;

/**
 * Created by Auser on 2016/11/15.
 */
public class TransactionsTest {
    static Trader raoul = new Trader("Raoul", "Cambridge");
    static Trader mario = new Trader("Mario","Milan");
    static Trader alan = new Trader("Alan","Cambridge");
    static Trader brian = new Trader("Brian","Cambridge");

    static List<Transaction> transactions = Arrays.asList(
            new Transaction(brian, 2011, 300),
            new Transaction(raoul, 2012, 1000),
            new Transaction(raoul, 2011, 400),
            new Transaction(mario, 2012, 710),
            new Transaction(mario, 2012, 700),
            new Transaction(alan, 2012, 950)
    );

    public static void main(String...args){
        test_1();
        test_2();
        test_3();
        test_4();
        test_5();
        test_6();
        test_7();
        test_8();
    }

    static void test_1(){
        List<Transaction> trans = transactions.stream()
                .filter(transaction -> 2011 == transaction.getYear())
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());
        System.out.println(trans.toString());
    }

    static void test_2(){
        List<String> citys = transactions.stream()
                .map(Transaction::getTrader)
                .map(Trader::getCity)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(citys.toString());
    }

    static void test_3(){
        List<String> names = transactions.stream()
                .map(Transaction::getTrader)
                .filter(trader -> "Cambridge".equals(trader.getCity()))
                .sorted(Comparator.comparing(Trader::getName))
                .map(trader -> trader.getName())
                .distinct()
                .collect(Collectors.toList());
        System.out.println(names);
    }

    static void test_4(){
        List<String> names = transactions.stream()
                .map(Transaction::getTrader)
                .map(trader -> trader.getName())
                .distinct()
                .sorted(String::compareTo)
                .collect(Collectors.toList());
        String strName = names.stream().collect(joining());
        System.out.println(names);
        System.out.println(strName);
    }

    static void test_5(){
        boolean hasMilan = transactions.stream()
                .anyMatch(transaction -> "Milan".equals(transaction.getTrader().getCity()));
        System.out.println(hasMilan);
        hasMilan = transactions.stream()
                .map(Transaction::getTrader)
                .anyMatch(trader -> "Milan".equals(trader.getCity()));
        System.out.println(hasMilan);
    }

    static void test_6(){
//        Stream<Trader> stream = transactions.stream()
//                .map(Transaction::getTrader)
//                .filter(trader -> "Cambridge".equals(trader.getCity()));
        Integer sum = transactions.stream()
                .filter(transaction -> "Cambridge".equals(transaction.getTrader().getCity()))
                .map(Transaction::getValue)
                .reduce(0, Integer::sum);
        System.out.println(sum);
    }

    static void test_7(){
        Integer max = transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max).get();
        System.out.println(max);
    }

    static void test_8(){
        Optional<Transaction> transaction = transactions.stream()
                .reduce((o1, o2) -> o1.getValue() < o2.getValue() ? o1 : o2);
        System.out.println(transaction.toString());
        transaction = transactions.stream()
                .min(Comparator.comparing(Transaction::getValue));
        System.out.println(transaction);
    }
}
